<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div>
        <label>Number A:<input id="radio-a" type="radio" name="math-obj" value="a" checked></label><input id="num-a"
            type="text">
        <label>Number B:<input id="radio-b" type="radio" name="math-obj" value="b" checked></label><input id="num-b"
            type="text">
    </div>
    <div>
        <button>判断当前选中的输入框输入内容是否为数字</button>
        <button>把 A 四舍五入为 B 个小数位数的数字</button>
        <button>当前选中数字的绝对值</button>
        <button>对当前选中的数字进行上舍入</button>
        <button>对当前选中的数字进行下舍入</button>
        <button>把当前选中的数字四舍五入为最接近的整数</button>
        <button>返回 A 和 B 中的最高值</button>
        <button>返回 A 和 B 中的最低值</button>
    </div>
    <p id="result"></p>

</body>
<script>
    var radioA = document.getElementById("radio-a");
    var radioB = document.getElementById("radio-b");
    var numA = document.getElementById("num-a");
    var numB = document.getElementById("num-b");
    var result = document.getElementById("result");
    var button = document.getElementsByTagName("button");

    /* radioA.addEventListener("click",radioClick);
    radioB.addEventListener("click",radioClick); */
    button[0].addEventListener("click", isnan);
    button[1].addEventListener("click", roundTo);
    button[2].addEventListener("click", absoluteValue);
    button[3].addEventListener("click", ceil);
    button[4].addEventListener("click", floor);
    button[5].addEventListener("click", round);
    button[6].addEventListener("click", max);
    button[7].addEventListener("click", min);
    //判断当前选中的输入框输入内容是否为数字
    function isnan() {
        if (radioA.checked) {
            if (isNaN(numA.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = "您输入的是数字"
            }
        } else if (radioB.checked) {
            if (isNaN(numB.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = "您输入的是数字"
            }
        }
    }

    /* 
           function isnan(){
            if(isNaN(numA.value||numB.value)){
                 result.innerHTML="您输入的不是数字"
            }else{
                result.innerHTML="您输入的是数字"
            }
          }
       
        function  radioClick(e){
            var button=document.getElementsByTagName("button");
            console.log(button);
            if(e.target.id=="radio-a"){
               
                   button[0].addEventListener("click",isnan);
            }
            else if(e.target.id=="radio-b"){
            
               button[0].addEventListener("click",isnan);
            }
        }  */

    //把 A 四舍五入为 B 个小数位数的数字
    function roundTo() {
        if (isNaN(numA.value && numB.value)) {
            result.innerHTML = "您输入的不是数字"
        } else {
            var newNum = Math.round(numA.value * (10 ** (numB.value))) / (10 ** (numB.value));
            result.innerHTML = newNum;
        }
    }
    //当前选中数字的绝对值
    function absoluteValue() {
        if (radioA.checked) {
            if (isNaN(numA.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.abs(numA.value);
            }
        } else if (radioB.checked) {
            if (isNaN(numB.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.abs(numB.value);
            }
        }
    }
    //对当前选中的数字进行上舍入
    function ceil() {
        if (radioA.checked) {
            if (isNaN(numA.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.ceil(numA.value);
            }
        } else if (radioB.checked) {
            if (isNaN(numB.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.ceil(numB.value);
            }
        }
    }
    //对当前选中的数字进行下舍入
    function floor() {
        if (radioA.checked) {
            if (isNaN(numA.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.floor(numA.value);
            }
        } else if (radioB.checked) {
            if (isNaN(numB.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.floor(numB.value);
            }
        }
    }
    // 把当前选中的数字四舍五入为最接近的整数
    function round() {
        if (radioA.checked) {
            if (isNaN(numA.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.round(numA.value);
            }
        } else if (radioB.checked) {
            if (isNaN(numB.value)) {
                result.innerHTML = "您输入的不是数字"
            } else {
                result.innerHTML = Math.round(numB.value);
            }
        }
    }
    //返回 A 和 B 中的最大值
    function max() {
        if (isNaN(numA.value && numB.value)) {
            result.innerHTML = "您输入的不是数字"
        } else {
            var max = numA.value > numB.value ? numA.value : numB.value
            result.innerHTML = "最大值是" + max;
        }
    }
    //返回 A 和 B 中的最小值
    function min() {
        if (isNaN(numA.value && numB.value)) {
            result.innerHTML = "您输入的不是数字"
        } else {
            var min = numA.value < numB.value ? numA.value : numB.value
            result.innerHTML = "最小值是" + min;
        }
    }
</script>

</html>